package com.tanqidi.arcgisandroid.ui

import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Environment
import android.os.Handler
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.tanqidi.arcgisandroid.R
import java.io.File

class LauncherActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (allPermissionsGranted()) {
            doAfterPermissionsGranted()
        } else {
            ActivityCompat.requestPermissions(this, PERMISSIONS_REQUIRED, PERMISSIONS_REQUEST_CODE)
        }
    }

    private fun createResourceFolder(){
        val appBasePath = Environment.getExternalStorageDirectory().absolutePath + File.separator + resources.getString(R.string.app_name) + File.separator
        listOf(
            getString(R.string.kml),
            getString(R.string.shp),
            getString(R.string.tpk),
            getString(R.string.gdb),
            getString(R.string.geojson),
            getString(R.string.project_data),
            getString(R.string.menu_project_config),
            getString(R.string.basemap_cache),
            getString(R.string.icon_resource),
            getString(R.string.export),
            getString(R.string.feature_img)
        ).forEach {
            val file = File(appBasePath + it)
            if(!file.exists()){
                file.mkdirs()
            }
        }
        //隐藏某些文件，不进入手机图库 底图缓存和图标资源
        listOf(getString(R.string.basemap_cache),getString(R.string.icon_resource)).forEachIndexed { index, item ->
            val nomedia = File(appBasePath + File.separator + item, ".nomedia")
            if(!nomedia.exists()){
                nomedia.createNewFile()
            }
        }

        //创建项目数据下面的3个子目录，shp，geojson，离线模式 BACKUPS
        listOf(getString(R.string.shp),getString(R.string.geojson),getString(R.string.geodatabase), getString(R.string.offline),getString(R.string.backups)).forEachIndexed { index, item ->
            val fileFolder = appBasePath + getString(R.string.project_data) + File.separator + item
            File(fileFolder).apply {
                if(!exists()){
                    mkdirs()
                }
            }
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        if (requestCode == PERMISSIONS_REQUEST_CODE) {
            if (allPermissionsGranted()) {
                doAfterPermissionsGranted()
            } else {
                Toast.makeText(this, "Permissions not granted by the user.", Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }

    private fun doAfterPermissionsGranted() {
        createResourceFolder()
        val intent = Intent(this, MainActivity::class.java)
        startActivity(intent)
        finish()
    }

    private fun allPermissionsGranted(): Boolean {
        for (permission in PERMISSIONS_REQUIRED) {
            if (ContextCompat.checkSelfPermission(baseContext, permission) != PackageManager.PERMISSION_GRANTED) {
                return false
            }
        }
        return true
    }

    override fun finish() {
        super.finish()
        overridePendingTransition(0, 0)
    }

    companion object {
        private const val PERMISSIONS_REQUEST_CODE = 10
        private val PERMISSIONS_REQUIRED = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_FINE_LOCATION
        )
    }
}